package com.fkcai.hansoul.model.vo.post;

import com.fkcai.hansoul.model.entity.PostCommentReply;
import com.fkcai.hansoul.model.entity.User;
import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

@Data
@Accessors(chain = true)
public class PostCommentReplyVo {
	private Integer replyId;          // 回复ID
	private String content;           // 回复内容
	private Integer likeCount;        // 点赞数
	private LocalDateTime createTime; // 创建时间
	private UserInfo author;      // 回复作者信息
	private UserInfo repliedUser; // 被回复用户信息
	private Integer commentId;        // 所属评论ID
	
	@Data
	public static class UserInfo {
		private Integer userId;          // 用户ID
		private String username;         // 用户名
		private String avatarUrl;        // 头像URL
		private Integer userLevel;       // 用户等级
		
		/**
		 * 从用户实体转换
		 */
		public static UserInfo fromUser (User user) {
			UserInfo userInfo = new UserInfo();
			
			userInfo.setUserId(user.getUserId());
			userInfo.setUsername(user.getUsername());
			userInfo.setAvatarUrl(user.getAvatarUrl());
			userInfo.setUserLevel(user.getUserLevel());
			
			return userInfo;
		}
	}
	
	/**
	 * 从回复实体和用户实体转换
	 */
	public static PostCommentReplyVo fromReply (
			PostCommentReply reply,
			User author,
			User repliedUser
	) {
		PostCommentReplyVo vo = new PostCommentReplyVo();
		
		vo.setReplyId(reply.getReplyId());
		vo.setContent(reply.getContent());
		vo.setLikeCount(reply.getLikeCount());
		vo.setCreateTime(reply.getCreateTime());
		vo.setCommentId(reply.getCommentId());
		
		if (author != null) {
			vo.setAuthor(UserInfo.fromUser(author));
		}
		
		if (repliedUser != null) {
			vo.setRepliedUser(UserInfo.fromUser(repliedUser));
		}
		
		return vo;
	}
	
}
